package com.probiz.estore.common.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.probiz.estore.Constants;
import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.common.service.ProductCategoryService;
import com.probiz.estore.eai.catalog.generated.InventoryClass;
import com.probiz.estore.eai.exception.EaiException;
import com.probiz.estore.product.ProductConstants;

public class ProductCategoryServiceImpl implements ProductCategoryService{
	protected final transient Log	logger	= LogFactory.getLog(getClass());
	
	private ProductCategoryManager productCategoryManager;

	public void setProductCategoryManager(
			ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}

	public void doImportCategoryForEai(InventoryClass inventoryClass) throws EaiException{
		ProductCategory category=productCategoryManager.getCategoryByCode(inventoryClass.getCode());
		if(null != category){
			//修改(会存在这样的业务：u8删除一个分类后又新加一个相同编码的分类，那么其生成的XML则丢失了这个过程，故外部系统无法处理该过程，只能作为修改看待)
			category.setCategoryName(inventoryClass.getName());
		}else{
			//新增
			//获得父目录编码长度
			if(!ProductConstants.EAI_CODE_RULE.containsKey(Integer.valueOf(inventoryClass.getCode().length()))){
				throw new EaiException("EAI inventoryClass 分类编码长度错误");
			}
			Integer parentCategoryCodeLength = ProductConstants.EAI_CODE_RULE.get(Integer.valueOf(inventoryClass.getCode().length()));
			ProductCategory parentCategory = null;
			if(parentCategoryCodeLength.intValue() == 0){
				//新增一级目录
				parentCategory = productCategoryManager.getById(Integer.valueOf(1));
			}else{
				//新增二、三、四、或五目录
				String parentCategoryCode = inventoryClass.getCode().substring(0,parentCategoryCodeLength.intValue());
				parentCategory = productCategoryManager.getCategoryByCode(parentCategoryCode);
				if(null == parentCategory){
					throw new EaiException("EAI inventoryClass 不存在该父目录:"+parentCategoryCode);
				}
			}
		
			category = new ProductCategory();
			category.setCategoryName(inventoryClass.getName());
			category.setCategoryCode(inventoryClass.getCode());
			category.setParentId(parentCategory.getCategoryId());
			//设置CateogryPath
			String parentCategoryPath=parentCategory.getCategoryPath();
			parentCategoryPath=parentCategoryPath==null?"":parentCategoryPath;
			category.setCategoryPath(parentCategoryPath+parentCategory.getCategoryId()+".");
			
			category.setStatus(Constants.FLAG_TRUE);
			category.setSortOrder(Integer.valueOf(1));
			//注意productType无法设置
		}
		logger.debug("导入： inventoryClass[name:"+inventoryClass.getName()+",code:"+inventoryClass.getCode()+"]");
		productCategoryManager.save(category);
		productCategoryManager.flush();
	}

	
	public void doImportCategoriesForEai(List<InventoryClass> inventoryClassList) throws EaiException{
		List<String> oldCategoryCodeList = productCategoryManager.getAllProductCategoryCodes();
		//新增或者修改
		for(InventoryClass inventoryClass : inventoryClassList){
			doImportCategoryForEai(inventoryClass);
			oldCategoryCodeList.remove(inventoryClass.getCode());
		}
		//逻辑删除
		for(String deleteCategoryCode : oldCategoryCodeList){
			logger.debug("delete inventoryClass[code"+deleteCategoryCode+"]");
			ProductCategory category = productCategoryManager.getCategoryByCode(deleteCategoryCode);
			category.setStatus(ProductCategory.STATUS_DELETED);
			productCategoryManager.save(category);
		}
		
	}
	

}
